<?php
    class WZ_Cache
    {
        public $_cache;
        static $_instance;
		
		// singleton
		public static function &getInstance(\Zend_Cache_Core $cache) 
		{
            if (!self::$_instance instanceof self) {
				self::$_instance = new self($cache);
			}
            return self::$_instance;
		}
        
        public function __construct(\Zend_Cache_Core $cache)
        {
            $this->_cache = $cache;
        }
        
        public function save($content, $key)
        {
            $content = gzdeflate(serialize($content), 9);
            $this->_cache->save($content, $key);
        }
        
        public function load($key)
        {
            if ($content = $this->_cache->load($key)) {
                return unserialize(gzinflate($content));
            }
            return false;
        }
        
        public static function cacheFunction()
        {
            $cache = \WZ::get('WZCacheApp');
            $argList = func_get_args();
            
            $function = $argList[0];
            $args = (isset($argList[1])) ? $argList[1] : array();
            $hash = md5($function . serialize($args));
            if (!$resCache = $cache->load($hash)) {
                $res = call_user_func_array($function, $args);
                $cache->save($res, $hash);
            } else {
                $res = $resCache;
            }
            return $res;
        }
        
        public static function cacheClassFunction()
        {
            $cache = \WZ::get('WZCacheApp');
            $argList = func_get_args();
            
            $class = $argList[0];
            $function = $argList[1];
            $args = (isset($argList[2])) ? $argList[2] : array();
            $hash = md5($class . $function . serialize($args));
            if (!$resCache = $cache->load($hash)) {
                $res = call_user_func_array(array($class, $function), $args);
                $cache->save($res, $hash);
            } else {
                $res = $resCache;
            }
            return $res;
        }
    }